package com.code;

import java.util.ArrayList;

import com.database.DBCallHistory;
import com.example.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CallHistory extends Activity {

	private DBCallHistory dbHelper;
	private ArrayList<String> detailHis;
	private ListView lvDetailHistory;
	private int REQUESTCODE_OPTION = 110;
	
	public CallHistory() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK){
			loadListView(dbHelper, detailHis, lvDetailHistory);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Aut o-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.callhistory);
		lvDetailHistory = (ListView) findViewById(R.id.lvDetailHistory);
		dbHelper = new DBCallHistory(this);
		loadListView(dbHelper, detailHis, lvDetailHistory);
//		dbHelper.open();
//		Cursor cur = dbHelper.SelectCallHisDetailTop();
//		detailHis = new ArrayList<String>();
//		if (cur != null) {
//			while (cur.moveToNext()) {
//				detailHis.add(0, cur.getString(0) + ". " + cur.getString(1)+": "+cur.getString(2));
////				detailHis.add(1, cur.getString(1));
//			}
//			if (detailHis.size() > 0) {
//			ArrayAdapter<String> arrAp = new ArrayAdapter<String>(this,
//					android.R.layout.simple_list_item_1, detailHis);
//			lvDetailHistory.setAdapter(arrAp);
				lvDetailHistory
						.setOnItemLongClickListener(new OnItemLongClickListener() {

							public boolean onItemLongClick(AdapterView<?> arg0,
									View v, int index, long arg3) {
								// TODO Auto-generated method stub
//								Log.d("in onLongClick");
								String valueItem = lvDetailHistory.getItemAtPosition(index).toString();
								Intent i = new Intent(CallHistory.this, MessageOption.class);
								Bundle b = new Bundle();
								b.putString("valueItem", valueItem);
								i.putExtras(b);
								startActivityForResult(i, REQUESTCODE_OPTION);
								return false;
							}

						});
//		}
//			cur.close();
//		}
//		dbHelper.close();
	}
	
	public void loadListView(DBCallHistory dbCallHis,ArrayList<String> detailHis ,ListView lvDetailHistory)
	{
		dbCallHis.open();	
		Cursor cur = dbCallHis.SelectCallHisDetailTop();
		detailHis = new ArrayList<String>();
		if (cur != null) {
			while (cur.moveToNext()) {
				detailHis.add(0, cur.getString(0) + ". " + cur.getString(1)+": "+cur.getString(2));
//				detailHis.add(1, cur.getString(1));
			}
			if (detailHis != null) {
			ArrayAdapter<String> arrAp = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, detailHis);
			lvDetailHistory.setAdapter(arrAp);
			}
			cur.close();
		}
		dbCallHis.close();
	}

}
